﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.ServiceModel;
using System.Web.Http;
using System.Net.Http;

using ERPStore.Extensions;

using CloudSoft.Extensions;

namespace ERPStore.Web.Controllers
{
	[System.Web.Http.Description.ApiExplorerSettings(IgnoreApi = true)]
	public class EmailTemplateApiController : ApiController
	{
		public EmailTemplateApiController(Services.IEmailTemplateService emailTemplateService)
		{
			EmailTemplateService = emailTemplateService;
		}

		protected Services.IEmailTemplateService EmailTemplateService { get; private set; }

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public ERPStore.Models.MailMessage GetTemplate(string templateName)
		{
			return EmailTemplateService.GetTemplate(templateName);
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		[System.Web.Http.HttpPost]
		public ERPStore.Models.MailMessage GetUserTemplate(ERPStore.Models.EmailTemplateRequest request)
		{
			ERPStore.Models.EmailTemplateRequest emailTemplateRequest = request;
			ERPStore.Models.MailMessage result = null;
			
			var userId = Convert.ToInt32(emailTemplateRequest.Parameters.Single(i => i.Name == "UserId").Value);

			result = EmailTemplateService.GetUserTemplate(emailTemplateRequest.TemplateName, userId, emailTemplateRequest.Parameters.ToDictionary());
			return result;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		[System.Web.Http.HttpPost]
		public ERPStore.Models.MailMessage GetOrderTemplate(ERPStore.Models.EmailTemplateRequest request)
		{
			var orderCode = request.Parameters.Single(i => i.Name == "OrderCode").Value;
			return EmailTemplateService.GetOrderTemplate(request.TemplateName, orderCode, request.Parameters.ToDictionary());
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		[System.Web.Http.HttpPost]
		public ERPStore.Models.MailMessage GetOrderCartTemplate(ERPStore.Models.EmailTemplateRequest request)
		{
			var code = request.Parameters.Single(i => i.Name == "OrderCartCode").Value;
			return EmailTemplateService.GetOrderCartTemplate(request.TemplateName, code);
		}


		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		[System.Web.Http.HttpPost]
		public List<ERPStore.Models.EmailTemplate> GetTemplateList()
		{
			return EmailTemplateService.GetTemplateList();
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		[System.Web.Http.HttpPost]
		public ERPStore.Models.EmailTemplate CreateCustomTemplate()
		{
			return EmailTemplateService.CreateCustomTemplate();
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpPost]
		[System.Web.Http.HttpDelete]
		public bool DeleteTemplate(ERPStore.Models.EmailTemplate TemplateName)
		{
			EmailTemplateService.DeleteTemplate(TemplateName);
			return true;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpPost]
		[System.Web.Http.HttpPut]
		public bool SaveTemplate(ERPStore.Models.EmailTemplate template)
		{
			EmailTemplateService.SaveTemplate(template);
			return true;
		}

		// [ActionFilters.ApiAuthorizedOperation]
		public IDictionary<string, string> GetTemplateListByModel(string model)
		{
			return EmailTemplateService.GetTemplateNameListByModel(model);
		}

	}
}
